//
//  HEDocImInfoView.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/27.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEDocImInfoView.h"
#import "HEIMGlobalDefs.h"
#import "HEIMChatViewController.h"
#import "HEIMPrivateDoctorObject.h"

@interface HEDocImInfoView ()

@property (nonatomic, strong) UIImageView *userView;
@property (nonatomic, strong) UILabel *nameLb;
@property (nonatomic, strong) UILabel *positionLb;
@property (nonatomic, strong) UILabel *timeLb;
@property (nonatomic, strong) UILabel *contentLB;
@property (nonatomic, strong) HEDoctorInfoModel *docInfo;

@end

@implementation HEDocImInfoView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    // 医生消息
    UIView *imWarp = [UIView new];
    imWarp.backgroundColor = [UIColor whiteColor];
    imWarp.layer.shadowColor = [UIColor colorWithRed:13/255.0 green:98/255.0 blue:218/255.0 alpha:0.08].CGColor;
    imWarp.layer.shadowOffset = CGSizeMake(0,5);
    imWarp.layer.shadowOpacity = 1;
    imWarp.layer.shadowRadius = 6;
    imWarp.layer.cornerRadius = 10;
    [self addSubview:imWarp];
    UITapGestureRecognizer *wapTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(intoChetClick)];
    [imWarp addGestureRecognizer:wapTap];
    
    [imWarp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).mas_offset(0);
        make.left.equalTo(self).mas_offset(0);
        make.right.equalTo(self).mas_offset(-0);
        make.bottom.equalTo(self).mas_offset(-10);
    }];
    
    _userView = [UIImageView new];
    [imWarp addSubview:_userView];
    
    _nameLb = [UILabel new];
    _nameLb.text = @"2191081人解读";
    _nameLb.font = DYBlodFont(18);
    [imWarp addSubview: _nameLb];
    
    _positionLb = [UILabel new];
    _positionLb.text = @"zhuren";
    _positionLb.font = DYFont(15);
    _positionLb.textColor = CNavSubTitleColor;
    [imWarp addSubview: _positionLb];
    
    _timeLb = [UILabel new];
    _timeLb.text = @"11111111";
    _timeLb.font = DYFont(15);
    _timeLb.textColor = CNavSubTitleColor;
    [imWarp addSubview: _timeLb];
    
    
    _contentLB = [UILabel new];
    _contentLB.text = @"11111111";
    _contentLB.font = DYFont(15);
    [imWarp addSubview: _contentLB];
    
    
    UIButton *emergBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [emergBtn setTitle:@"紧急电话" forState:UIControlStateNormal];
    emergBtn.titleLabel.font = DYBlodFont(14);
    emergBtn.backgroundColor = CPriceColor;
    [imWarp addSubview:emergBtn];
    [emergBtn addTarget:self action:@selector(callPhoneClick) forControlEvents:UIControlEventTouchUpInside];

    
    UIButton *consulBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [consulBtn setTitle:@"在线咨询" forState:UIControlStateNormal];
    consulBtn.titleLabel.font = DYBlodFont(14);
    consulBtn.backgroundColor = CNavBgColor;
    [imWarp addSubview:consulBtn];
    [consulBtn addTarget:self action:@selector(intoChetClick) forControlEvents:UIControlEventTouchUpInside];
    
    [_userView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imWarp).offset(15);
        make.left.equalTo(imWarp).offset(15);
        make.height.width.offset(70);
    }];
    [_userView hg_setAllCornerWithCornerRadius: 35];
    
    [_nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imWarp).offset(15);
        make.left.equalTo(_userView.mas_right).offset(10);
    }];
    [_nameLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    
    [_positionLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_nameLb.mas_bottom).offset(0);
        make.left.equalTo(_nameLb.mas_right).offset(10);
    }];
    
    [_timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_nameLb.mas_bottom).offset(0);
        make.right.equalTo(imWarp).offset(-10);
    }];
    
    [_contentLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nameLb.mas_bottom).offset(10);
        make.left.equalTo(_userView.mas_right).offset(10);
        make.right.equalTo(imWarp).offset(-10);
    }];
    
    [emergBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentLB.mas_bottom).offset(10);
        make.left.equalTo(_userView.mas_right).offset(10);
        make.width.offset([@"紧急电话" widthForFont:DYBlodFont(15)]  + 10);
        make.height.offset(26);
        make.bottom.equalTo(imWarp).offset(-10);
    }];
    [emergBtn hg_setAllCornerWithCornerRadius:4];
    [consulBtn hg_setAllCornerWithCornerRadius:4];

    [consulBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentLB.mas_bottom).offset(10);
        make.left.equalTo(emergBtn.mas_right).offset(10);
        make.width.offset([@"紧急电话" widthForFont:DYBlodFont(15)] + 10);
        make.height.offset(26);
        make.bottom.equalTo(imWarp).offset(-10);
    }];
}

- (void)callPhoneClick {
    [DYTools callPhone: _docInfo.phone];
}
 
- (void)intoChetClick {
    HEIMKitInfo *doc = [HEIMKitInfo new];
    doc.infoId = _docInfo.user_id;
    doc.showName = _docInfo.realname;
    doc.avatarUrlString = _docInfo.avatar;
    doc.avatarImage = IMAGE_NAMED(@"hmang");
    
    UserInfo *user = [[UserManager sharedUserManager] getUserInfo];
    HEIMKitInfo *owner = [HEIMKitInfo new];
    owner.infoId = user.id;
    owner.showName = user.realname;
    owner.avatarUrlString = user.avatar;
    owner.avatarImage = IMAGE_NAMED(@"defualt_icon");
    
    [[HEIMKit sharedKit] registeUserInfos: @[doc, owner]];

    HEIMChatViewController *imVC = [HEIMChatViewController new];
    imVC.docId = _docInfo.user_id;
    [_vc.navigationController pushViewController:imVC animated:YES]; 
}

- (void)configIMModel: (HEIMInfoEntity *) model docModel:(HEDoctorInfoModel *)docInfo {
    _model = model;
    _docInfo = docInfo;
    
    _nameLb.text = docInfo.realname;
    _positionLb.text = docInfo.post_name;
    
    NSString *cntStr = @"";
    if (model.contentModel.type == HEIMMessageTypeImage) {
        cntStr = @"[图片]";
    }else if (model.contentModel.type == HEIMMessageTypeAudio) {
        cntStr = @"[语音消息]";
    }else if (model.contentModel.type == HEIMMessageTypeConsulReport) {
        cntStr = @"[咨询报告]";
    }else if ([DYTools isBlankString:model.content]) { // 私人医生签约信息
        HEIMPrivateDoctorObject *private = [HEIMPrivateDoctorObject mj_objectWithKeyValues:model.extras];
        cntStr = private.disease_desc;
    }else {
        cntStr = model.contentModel.content;
    }
    
    if (model.contentModel.actor == HEActorTypeUser || [DYTools isBlankString:model.content]) {
        cntStr = [NSString stringWithFormat:@"我：%@", cntStr];
    }else {
        cntStr = [NSString stringWithFormat:@"%@：%@", docInfo.realname, cntStr];
    }
    _contentLB.text = cntStr;

    _timeLb.text = [NSDate timestampToDate:model.create_date formaterMode: NSDateFormaterModeDayNoYear];
    [_userView sd_setImageWithURL:[NSURL URLWithString:docInfo.avatar] placeholderImage:IMAGE_NAMED(@"defualt_icon")];
}
@end
